//
//  ViewController.swift
//  paomadeng
//
//  Created by 初光夫 on 2021/11/17.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var cusSwitch: UISwitch!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var showTitle: UILabel!
    @IBOutlet weak var rSlider: UISlider!
    @IBOutlet weak var gSlider: UISlider!
    @IBOutlet weak var bSlider: UISlider!
    @IBOutlet weak var timeField: UITextField!
    @IBOutlet weak var disField: UITextField!
    
    var rnum: CGFloat = 0.0
    var gnum: CGFloat = 0.0
    var bnum: CGFloat = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        creatSubView()
    }

    func creatSubView() {
        textField.addTarget(self, action: #selector(textFieldTextChanged(tf:)), for: .editingChanged)
        cusSwitch.addTarget(self, action: #selector(cusSwitchChanged(sw:)), for: .valueChanged)
        rSlider.addTarget(self, action: #selector(sliderValueChanged(slid:)), for: .valueChanged)
        gSlider.addTarget(self, action: #selector(sliderValueChanged(slid:)), for: .valueChanged)
        bSlider.addTarget(self, action: #selector(sliderValueChanged(slid:)), for: .valueChanged)
    }
    
    @objc func textFieldTextChanged(tf: UITextField) {
        showTitle.text = tf.text
    }
    
    @objc func cusSwitchChanged(sw: UISwitch) {
        if sw.isOn {
            //bg
            rSlider.value = Float(readRGBFromColor(bgView.backgroundColor!)[0])
            gSlider.value = Float(readRGBFromColor(bgView.backgroundColor!)[1])
            bSlider.value = Float(readRGBFromColor(bgView.backgroundColor!)[2])
        }else {
            //text
            rSlider.value = Float(readRGBFromColor(showTitle.textColor)[0])
            gSlider.value = Float(readRGBFromColor(showTitle.textColor)[1])
            bSlider.value = Float(readRGBFromColor(showTitle.textColor)[2])
        }
    }
    
    @objc func sliderValueChanged(slid: UISlider) {
        if slid == rSlider {
            rnum = CGFloat(slid.value)
        }else if slid == gSlider {
            gnum = CGFloat(slid.value)
        }else if slid == bSlider {
            bnum = CGFloat(slid.value)
        }
        
        if cusSwitch.isOn {
            bgView.backgroundColor = UIColor.init(red: rnum, green: gnum, blue: bnum, alpha: 1)
        }else {
            showTitle.textColor = UIColor.init(red: rnum, green: gnum, blue: bnum, alpha: 1)
        }
    }
    
    @IBAction func endEditingClick(_ sender: Any) {
        view.endEditing(true)
    }
    
    @IBAction func startClick(_ sender: Any) {
        if textField.text?.count == 0 {
            let alertVc = UIAlertController.init(title: "请输入文字", message: nil, preferredStyle: .alert)
            alertVc.addAction(UIAlertAction.init(title: "确定", style: .cancel, handler: nil))
            self.present(alertVc, animated: true, completion: nil)
            return
        }
        
        let vc = StartViewController()
        vc.bgColor = bgView.backgroundColor
        vc.cusTitle = showTitle.text!
        vc.cusFont = showTitle.font
        vc.cusTitleColor = showTitle.textColor
        if let str = timeField.text, str.count > 0 {
            vc.duration = Int(str)!
        }
        if let str = disField.text, str.count > 0 {
            vc.fontSize = Double(str)!
        }
        vc.modalPresentationStyle = .currentContext
        vc.modalTransitionStyle = .crossDissolve
        self.definesPresentationContext = true
        self.present(vc, animated: true, completion: nil)
    }
    
    @IBAction func selectedFont(_ sender: Any) {
        let vc = FontViewController()
        vc.modalPresentationStyle = .fullScreen
        vc.fontCallbackBlock = { [weak self] fontStr in
            self?.showTitle.font = UIFont.init(name: fontStr, size: 30)
        }
        self.present(vc, animated: true, completion: nil)
    }
    
    func readRGBFromColor(_ color: UIColor) -> [CGFloat] {
        let components = color.cgColor.components
        guard let arr = components else {
            return [0, 0, 0]
        }
        if arr.count == 1 {
            return [arr[0], arr[0], arr[0]]
        }else if arr.count == 2 {
            return [arr[0], arr[0], arr[0]]
        }else {
            return [arr[0], arr[1], arr[2]]
        }
    }
}

